from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By

from common.Log import Log


class Base:
    """selenium3"""
    def __init__(self, driver):
        self.driver = driver
        self.log = Log().logging()

    def findele(self, *args):
        try:
            print(args)
            self.log.info("通过"+args[0]+"定位，元素是"+args[1])
            return self.driver.find_element(*args)
        except:
            self.log.info("定位元素失败")

    def click(self, args):
        self.findele(args).click()

    def sendkey(self, args, value):
        self.findele(args).send_keys(value)

    def js(self, str):
        self.driver.execute_script(str)

    def url(self):
        return self.driver.current_url

    def back(self):
        self.driver.back()

    def forword(self):
        self.driver.forword()

    def quit(self):
        self.driver.quit()


class Test(Base):
    def __init__(self, driver):
        Base.__init__(self, driver)
        self.driver.get("https://www.baidu.com")

    def search(self):
        _input = (By.ID, 'kw')
        self.findele(_input)
        self.sendkey(_input, 'selenium')
        _btn = (By.ID, 'su')
        self.click(_btn)
        sleep(3)
        self.log.info('success')


if __name__ == "__main__":
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(10)
    Test(driver).search()